/*---------------------------------------------------------------------------*
 * Copyright (C) 2011-2014 Ryan M. Murphy <ryan.m.murphy.77@gmail.com>
 *
 * File Name   : usr_appInitBms.c
 *
 * Project     : Watts-rock
 *               <http://code.google.com/p/watts-rock-motherboard/>
 *
 * Description : This file is responsible for initializing the arbitros
 *               scheduler, any drivers needed by the user application, and
 *               all user-space threads.
 *
 * Last Update : Jan 4, 2014
 *---------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------*
 * Include Files
 *---------------------------------------------------------------------------*/
#include <stdio.h>
#include <stdio.h>
#include "arb_scheduler.h"
#include "arb_printf.h"
#include "arb_console.h"
#include "utl_pmic.h"
#include "utl_clocks.h"
#include "utl_pmic.h"
#include "utl_clocks.h"
#include "drv_signal.h"
#include "drv_console.h"
#include "drv_bms.h"
#include "drv_lcd.h"
#include "drv_wifly.h"
#include "drv_sd.h"
#include "usr_consoleBms.h"
#include "usr_bms.h"
#include "usr_hmi.h"
#include "usr_wiFly.h"

/*---------------------------------------------------------------------------*
 * Private Defines
 *---------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------*
 * Private Function Prototypes
 *---------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------*
 * Private Global Variables
 *---------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------*
 * Private Function Definitions
 *---------------------------------------------------------------------------*/
static void usr_driverRegistration( void)
{
   t_signalSetup t_sigSetup;
   t_consoleSetup t_conSetup;
   t_bmsSetup t_bSetup;
   t_lcdSetup t_lSetup;
   t_sdSetup t_sdCardSetup;
   t_wiflySetup t_wSetup;

   /*------------------------------------------------------------------------*
    * Register the console driver with the kernel.
    *------------------------------------------------------------------------*/
   t_conSetup.c_uartId   = UART_1;
   t_conSetup.i_baudRate = 115200;
   t_conSetup.c_majorNum = 1;

   if( drv_consoleInit( t_conSetup) < 0)
      exit(0);

   /*------------------------------------------------------------------------*
    * Register the LED signaling driver with the kernel.
    *------------------------------------------------------------------------*/
   t_sigSetup.c_signalPort  = GPIO_PORTK;
   t_sigSetup.ac_ledPins[0] = PIN_0; /*Red LED pin*/
   t_sigSetup.ac_ledPins[1] = PIN_1; /*Yellow LED pin*/
   t_sigSetup.ac_ledPins[2] = PIN_2; /*Green LED pin*/
   t_sigSetup.c_majorNum    = 2;

   if( drv_signalInit( t_sigSetup) < 0)
      exit(0);

   /*------------------------------------------------------------------------*
    * Register the battery management system driver with the kernel.
    *------------------------------------------------------------------------*/
   t_bSetup.t_battVoltAdc    = ADC1_MODULE;
   t_bSetup.c_battVoltPosPin = PIN_4;
   t_bSetup.c_battVoltNegPin = PIN_3;
   t_bSetup.t_battCurrAdc    = ADC2_MODULE;
   t_bSetup.c_battCurrPosPin = PIN_4;
   t_bSetup.c_battCurrNegPin = PIN_3;
   t_bSetup.t_tempAdc        = ADC2_MODULE;
   t_bSetup.c_tempPosPin     = PIN_5;
   t_bSetup.c_tempNegPin     = PIN_2;
   t_bSetup.t_pacCurrAdc     = ADC1_MODULE;
   t_bSetup.c_pacCurrPosPin  = PIN_5;
   t_bSetup.c_pacCurrNegPin  = PIN_2;
   t_bSetup.c_shutdownPort   = GPIO_PORTH;
   t_bSetup.c_shutdownPin    = PIN_3;
   t_bSetup.c_fanPort        = GPIO_PORTH;
   t_bSetup.c_fanPin         = PIN_5;
   t_bSetup.c_pacFetPort     = GPIO_PORTD;
   t_bSetup.c_pacFetPin      = PIN_0;
   t_bSetup.c_majorNum       = 3;

   if( drv_bmsInit( t_bSetup) < 0)
      exit(0);

   /*------------------------------------------------------------------------*
    * Register the 4x20 character 8-bit LCD driver with the kernel.
    *------------------------------------------------------------------------*/
   t_lSetup.c_uartId     = UART_5;
   t_lSetup.i_baudRate   = 9600;
   t_lSetup.c_numRows    = 4;
   t_lSetup.c_numColumns = 20;
   t_lSetup.c_majorNum   = 4;

   if( drv_lcdInit( t_lSetup) < 0)
      exit(0);

   /*------------------------------------------------------------------------*
    * Register the sd card driver with the kernel.
    *------------------------------------------------------------------------*/
   t_sdCardSetup.t_csPort   = GPIO_PORTC;
   t_sdCardSetup.t_csPin    = PIN_4;
   t_sdCardSetup.t_spiChan  = SPI_1;
   t_sdCardSetup.c_majorNum = 5;

   if( drv_sdInit( t_sdCardSetup) < 0)
      exit(0);

   /*------------------------------------------------------------------------*
    * Register the wiFly driver with the kernel.
    *------------------------------------------------------------------------*/
   t_wSetup.c_uartId    = UART_3;
   t_wSetup.i_baudRate  = 9600;
   t_wSetup.c_resetPort = GPIO_PORTD;
   t_wSetup.c_resetPin  = PIN_4;
   t_wSetup.c_majorNum  = 6;

   if( drv_wiflyInit( t_wSetup) < 0)
   {
      exit(0);
   }

   /*------------------------------------------------------------------------*
    * Add additional drivers here...
    *------------------------------------------------------------------------*/

}/*End usr_driverRegistration*/

static void usr_platformInit( void)
{

   /*------------------------------------------------------------------------*
    * Configure CPU clock for 32Mhz operation
    *------------------------------------------------------------------------*/
   utl_setCpuFreq( 32000000);

   /*------------------------------------------------------------------------*
    * Configure the highest level of interrupts - nesting is not allowed.
    *------------------------------------------------------------------------*/
   utl_configureIntLevel( INT_LEVEL_2);

   /*------------------------------------------------------------------------*
    * Register drivers with the kernel...
    *------------------------------------------------------------------------*/
   usr_driverRegistration();

   /*------------------------------------------------------------------------*
    * Initialize the debug interface.
    *------------------------------------------------------------------------*/
   if( arb_printfInit( "consoleDevice0",
                       300,
                       true) < 0)
   {
      exit(0);
   }

   /*------------------------------------------------------------------------*
    * Initialize the console interface.
    *------------------------------------------------------------------------*/
   if( arb_consoleInit( "consoleDevice0",
                        ARB_STACK_1024B,
                        254,
                        &usr_console) < 0)
   {
      exit(0);
   }

   /*------------------------------------------------------------------------*
    * Initialize the scheduler specifically for this application.
    *------------------------------------------------------------------------*/
   if( arb_schedulerInit( PRIORITY,
                          ARB_STACK_512B, /*Idle thread size*/
                          TIMER_7) < 0)
   {
	   exit(0);
   }

}/*End usr_platformInit*/

/*---------------------------------------------------------------------------*
 * Public Function Definitions
 *---------------------------------------------------------------------------*/
void usr_appInit( void)
{

   /*------------------------------------------------------------------------*
    * Enable global interrupts, configure the CPU frequency, and register
    * drivers with the kernel-specific for this particular computing platform.
    *------------------------------------------------------------------------*/
   usr_platformInit();

   /*------------------------------------------------------------------------*
    * Initialize all the user space applications here...
    *------------------------------------------------------------------------*/
   if( usr_bmsInit() < 0)
   {
      exit(0);
   }

   if( usr_wiFlyInit() < 0)
   {
     exit(0);
   }

   /*------------------------------------------------------------------------*
    * Once we have exited this function the OS will begin running.
    *------------------------------------------------------------------------*/

}/*End usr_appInit*/

